package com.example.match.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.match.R;
import com.example.mylibrary.util.CommonUtil;
import com.example.mylibrary.util.DateUtils;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/3/26.
 */

public class FileAdapter extends BaseAdapter {
    private Context context;
    private List<File> files;

    public FileAdapter(Context context, List<File> files) {
        this.context = context;
        this.files = files;
    }

    @Override
    public int getCount() {
        return files.size();
    }

    @Override
    public Object getItem(int position) {
        return files.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_lv_file, null);
            holder.ivFile = (ImageView) convertView.findViewById(R.id.iv_file);
            holder.tvFileName = (TextView) convertView
                    .findViewById(R.id.tv_fileName);
            holder.tvFileDate = (TextView) convertView
                    .findViewById(R.id.tv_file_date);
            holder.tvFileSize = (TextView) convertView
                    .findViewById(R.id.tv_file_size);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        File file = files.get(position);
        if (null != file) {
            holder.tvFileName.setText(file.getName());
            holder.tvFileDate.setText(
                    DateUtils.getDateFromMillSeconds(file.lastModified()));
            holder.tvFileSize
                    .setText(CommonUtil.getStingSizeFromByte(file.length()));
            if (file.isDirectory()) {// 目录
                holder.ivFile.setImageDrawable(context.getResources()
                        .getDrawable(R.drawable.folder));
            } else {// 非目录
                holder.ivFile.setImageDrawable(context.getResources()
                        .getDrawable(R.drawable.file));
            }
        }
        return convertView;
    }
    public class ViewHolder {
        public ImageView ivFile;
        public TextView tvFileName;
        public TextView tvFileDate;
        public TextView tvFileSize;
    }
}
